package com.yxx.sys.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yxx.sys.common.Constast;
import com.yxx.sys.common.DataGridView;
import com.yxx.sys.domain.Dept;
import com.yxx.sys.domain.User;
import com.yxx.sys.service.DeptService;
import com.yxx.sys.service.UserService;
import com.yxx.sys.vo.UserVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yxx
 * @since 2020-08-12
 */
@RestController
@RequestMapping("/user")
public class UserController {


  @Autowired
  UserService userService;

  @Autowired
  DeptService deptService;

  /***
   *@Description: 全查询
   * @param userVO
   * @return: com.yxx.sys.common.DataGridView
   **/
  @GetMapping("/loadAllUser")
  @ApiOperation(value="查询用户信息接口", notes="查询用户信息接口")
  public DataGridView loadAllUser(UserVO userVO) {
    Page<User> page = new Page<>(userVO.getPage(),userVO.getLimit());
    QueryWrapper<User> queryWrapper = new QueryWrapper<>();
    queryWrapper.like(StrUtil.isNotEmpty(userVO.getName()),"name",userVO.getName()).or().eq(StrUtil.isNotEmpty(userVO.getName()),"loginname",userVO.getName());
    queryWrapper.like(StrUtil.isNotEmpty(userVO.getAddress()),"address",userVO.getAddress());
    queryWrapper.eq("type", Constast.USER_TYPE_NORMAL); //查询管理员
    queryWrapper.eq(userVO.getDeptid()!=null,"deptid",userVO.getDeptid());
    userService.page(page, queryWrapper);

    List<User> users = page.getRecords();
    for (User user : users) {
      Integer deptid = user.getDeptid();
      if(deptid!=null){
        Dept dept = deptService.getById(deptid);
        user.setDeptName(dept.getTitle());
      }
      Integer mgr = user.getMgr();
      if(mgr!=null){
        User one = userService.getById(mgr);
        user.setLeaderName(one.getName());
      }
    }
    return new DataGridView(page.getTotal(),users);
  }
}
